<template>
  <div>
    <el-dialog
      width="600px"
      :title="type == 'add' ? $t('新增') : $t('修改')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form
        @submit.native.prevent
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item :label="$t('快递公司名')" prop="expressName">
          <el-input v-model="form.expressName" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('类型')" prop="type">
          <el-select style="width: 100%" v-model="form.type" :placeholder="$t('请选择')">
            <!--  1-头程 2-中程 3-尾程 -->
            <el-option :label="$t('头程')" :value="1" />
            <el-option :label="$t('中程')" :value="2" />
            <el-option :label="$t('尾程')" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('负责人')" prop="admin">
          <el-input v-model="form.admin" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('电话')" prop="tel">
          <el-input v-model="form.tel" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('查询地址')" prop="net">
          <el-input v-model="form.net" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('计费方式')" prop="billingMethod">
          <el-input type="textarea" :rows="3" v-model="form.billingMethod" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('结算方式')" prop="settlementMethod">
          <el-input type="textarea" :rows="3" v-model="form.settlementMethod" :placeholder="$t('请输入')" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          v-hasPermi="['distribution:expressCompany:add']"
          @click="handleSubmit"
          type="primary"
          v-show="type == 'add'"
        >
          {{ $t('确认新增') }}
        </el-button>
        <el-button
          v-hasPermi="['distribution:expressCompany:edit']"
          @click="handleSubmit"
          type="primary"
          v-show="type == 'edit'"
        >
          {{ $t('确认修改') }}
        </el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addExpressCompany, updateExpressCompany } from '@/api/basicSetting/expressCompany'

export default {
  props: {
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        expressName: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        type: [{ required: true, message: this.$t('请选择'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        Object.keys(this.formData).forEach((key) => {
          this.$set(this.form, key, this.formData[key] || '')
        })
      }
    },
  },
  methods: {
    getList() {},

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.type == 'add') {
            const data = {
              ...this.form,
            }
            addExpressCompany(data).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.$emit('getList', false)
                this.cancel()
              }
            })
          }
          if (this.type == 'edit') {
            const data = {
              ...this.form,
            }
            updateExpressCompany(data).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.$emit('getList', false)
                this.cancel()
              }
            })
          }
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
